<?php

class GcmController extends Controller {
    
    
    const SERVER_KEY = 'AIzaSyCaHSOBwdT3PT7Hx1MBM_j9EhplDK5klLY';
    const GCM_SERVER = 'https://android.googleapis.com/gcm/send';

    public function actionReg() {
        $user_id = Yii::app()->user->id;
        $gcm_id = Yii::app()->request->getParam('gcm_id');

        $gcm = Gcm::model()->find('user_id = :user_id AND gcm_id = :gcm_id', array(':user_id' => $user_id, ':gcm_id' => $gcm_id));

        if ($gcm)
            $this->end(array('result' => -1));

        $gcm = new Gcm();
        $gcm->user_id = $user_id;
        $gcm->gcm_id = $gcm_id;

        if ($gcm->save())
            $this->end(array('result' => 1));
        else
            $this->end(array('result' => -1, 'errors' => $gcm->errors));
    }

    public function actionUnreg() {
        $user_id = null;
        $gcm_id = Yii::app()->request->getParam('gcm_id');

        $gcm = Gcm::model()->find('gcm_id = :gcm_id', array(':gcm_id' => $gcm_id));

        if ($gcm) {
            if ($gcm->delete())
                $this->end(array('result' => 0));
            else
                $this->end(array('result' => -1, 'errors' => $gcm->errors));
        } else
            $this->end(array('result' => -2));
    }

    
    /**
     * sendGCMAction
     *
     * Send messages to Google Cloud Messaging Server
     *
     * @param int $user_id
     * @param string $message
     *
     * @return mixed
     */
    public static function actionSendGCM() {
        $user_id = Yii::app()->request->getParam('user_id');
        $message = "Nothing";
        $gcm_ids = Yii::app()->db->createCommand()
                ->select('gcm_id')
                ->from('gcm')
                ->where('user_id = :user_id')
                ->queryAll(true, array('user_id' => $user_id));
        
        $a = array($gcm_ids[0]['gcm_id']);
        if ($gcm_ids) {
            $headers = array(
                'Content-Type:application/json',
                'Authorization:key=' . GcmController::SERVER_KEY
            );

            $data = array(
                'registration_ids' => $a,
                'data' => array('message' => $message)
            );

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, GcmController::GCM_SERVER);
            if ($headers)
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

            $response = curl_exec($ch);

            curl_close($ch);
            die ($response);
        } else {
            return -1;
        }
    }

    public function filters() {
        return array(
            //'accessControl',
        );
    }

    public function accessRules() {
        return array(
            array('allow', // allow authenticated users to access all actions　　（　されたユーザーはすべてのアクションへのアクセスを許可する　）
                'users' => array('@'),
            ),
            array('deny', // deny all users　(すべてのユーザーを拒否する。)
                'users' => array('*'),
            ),
        );
    }

}